package com.zws.cucumber.expander;

import com.zws.cucumber.var.ZwsVar;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import static com.zws.cucumber.expander.ZwsStrExpander.ParamEmptyString;

public class ZwsStrArgFirstMap implements Map<String, Object> {

    private Map<String, Object> argumentMap;
    private Map<String, Object> localVariables;

    public ZwsStrArgFirstMap(Map<String, Object> argumentMap, Map<String, Object> map) {
        this.argumentMap = argumentMap;
        if (map instanceof ZwsStrArgFirstMap parent) {
            this.localVariables = parent.getLocalVariables();
        } else {
            this.localVariables = map;
        }
    }

    public Map<String, Object> getLocalVariables() {
        return localVariables;
    }

    public boolean containsInArgumentMap(String key) {
        return argumentMap.containsKey(key);
    }

    public boolean containsInLocalVariables(String key) {
        return localVariables != null && localVariables.containsKey(key);
    }

    @Override
    public int size() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean isEmpty() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsKey(Object key) {
        return argumentMap.containsKey(key)
                || StringUtils.isNumeric((CharSequence) key)
                || (localVariables != null && localVariables.containsKey(key))
                || ZwsVar.containsKey((String) key);
    }

    @Override
    public boolean containsValue(Object value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object get(Object key) {
        if (argumentMap.containsKey((String) key)) {
            return argumentMap.get(key);
        }
        if (StringUtils.isNumeric((CharSequence) key)) {
            return ParamEmptyString;
        }
        if (localVariables != null) {
            return localVariables.get(key);
        }
        return ZwsVar.get((String) key);
    }

    @Override
    public Object put(String key, Object value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object remove(Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<String> keySet() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection<Object> values() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<Entry<String, Object>> entrySet() {
        throw new UnsupportedOperationException();
    }
}
